package com.ruoyi.system.service.impl;

import java.util.*;
import java.util.stream.Collectors;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.Assess;
import com.ruoyi.system.domain.vo.SysDeptVO;
import com.ruoyi.system.domain.vo.SysUserVO;
import com.ruoyi.system.mapper.AssessMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.AssessUserMapper;
import com.ruoyi.system.domain.AssessUser;
import com.ruoyi.system.service.IAssessUserService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2025-04-24
 */
@Service
public class AssessUserServiceImpl implements IAssessUserService {

    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private AssessUserMapper assessUserMapper;
    @Autowired
    private AssessMapper assessMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param id 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public AssessUser selectAssessUserById(Long id) {
        return assessUserMapper.selectAssessUserById(id);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param assessUser 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<AssessUser> selectAssessUserList(AssessUser assessUser) {
        return assessUserMapper.selectAssessUserList(assessUser);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param assessUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertAssessUser(AssessUser assessUser) {
        return assessUserMapper.insertAssessUser(assessUser);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param assessUser 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateAssessUser(AssessUser assessUser) {
        return assessUserMapper.updateAssessUser(assessUser);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param ids 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAssessUserByIds(Long[] ids) {
        return assessUserMapper.deleteAssessUserByIds(ids);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param id 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteAssessUserById(Long id) {
        return assessUserMapper.deleteAssessUserById(id);
    }

    @Override
    public int deleteAssessUserByTaskId(Long taskId) {
        return assessUserMapper.deleteAssessUserByTaskId(taskId);
    }

    @Override
    public List<Map<String, Object>> selectAssessListByTaskId(Long taskId, Integer type) {

        Assess assess = assessMapper.selectAssessById(taskId);
        if (assess == null) {
            throw new ServiceException("未找到该任务");
        }
        List<Map<String, Object>> result = new ArrayList<>();

        if(type == 7){
            List<Long> userIds = new ArrayList<>();
            if(assess.getOpenUnit().equals(type.toString())){
                String[] ids = assess.getCreateUser().split(",");
                userIds = Arrays.stream(ids)
                        .map(String::trim) // 去除字符串两端的空格（如果有的话）
                        .map(Long::valueOf) // 将字符串转换为 Long 类型
                        .collect(Collectors.toList()); // 收集为 List
            }else {
                String[] ids = assess.getCreateDept().split(",");
                List<Long> deptIds = Arrays.stream(ids)
                        .map(String::trim) // 去除字符串两端的空格（如果有的话）
                        .map(Long::valueOf) // 将字符串转换为 Long 类型
                        .collect(Collectors.toList()); // 收集为 List
                userIds = sysUserService.selectUserListByDeptIds(deptIds);
            }
        }

        if (StringUtils.isEmpty(assess.getCreateUser())) {
            String[] ids = assess.getCreateUser().split(",");
            List<Long> userIds = Arrays.stream(ids)
                    .map(String::trim) // 去除字符串两端的空格（如果有的话）
                    .map(Long::valueOf) // 将字符串转换为 Long 类型
                    .collect(Collectors.toList()); // 收集为 List
            List<SysUser> users = sysUserService.selectUserListByUserIds(userIds);
            for (SysUser sysUser : users) {
                Map<String, Object> userMap = new HashMap<>();
                SysDeptVO sysDeptVO = sysDeptService.selectDeptVoById(sysUser.getDeptId());
                userMap.put("id", sysUser.getUserId());
                userMap.put("name", sysUser.getNickName());
                userMap.put("type", type);
                userMap.put("sex", sysUser.getSex());
                userMap.put("city", sysDeptVO.getCity());
                userMap.put("region", sysDeptVO.getRegion());
                userMap.put("grade", sysDeptVO.getGrade());
                userMap.put("school", sysDeptVO.getSchool());
                userMap.put("classz", sysDeptVO.getClassz());
                userMap.put("education", sysDeptVO.getEducation());
                result.add(userMap);
            }
        } else {
            String[] ids = assess.getCreateDept().split(",");
            List<Long> deptIds = Arrays.stream(ids)
                    .map(String::trim) // 去除字符串两端的空格（如果有的话）
                    .map(Long::valueOf) // 将字符串转换为 Long 类型
                    .collect(Collectors.toList()); // 收集为 List
            List<SysDept> depts = sysDeptService.selectDeptByDeptIds(deptIds, null);
            for (SysDept sysDept : depts) {
                Map<String, Object> userMap = new HashMap<>();
                SysDeptVO sysDeptVO = sysDeptService.selectDeptVoById(sysDept.getDeptId());
                userMap.put("id", sysDept.getDeptId());
                userMap.put("name", sysDept.getDeptName());
                userMap.put("email", sysDept.getEmail());
                userMap.put("leader", sysDept.getLeader());
                userMap.put("phone", sysDept.getPhone());
                userMap.put("type", type);
                userMap.put("city", sysDeptVO.getCity());
                userMap.put("region", sysDeptVO.getRegion());
                userMap.put("grade", sysDeptVO.getGrade());
                userMap.put("school", sysDeptVO.getSchool());
                userMap.put("classz", sysDeptVO.getClassz());
                userMap.put("education", sysDeptVO.getEducation());
                result.add(userMap);
            }
        }
        return result;
    }
}
